import { defineStore } from 'pinia';
import { useAutoAction, useAutoGetter } from './autoActionGetter.js';
import { useUserInfoStore } from './userInfo.js';
import { handleCommonRes } from '../utils/resolver.js';
import { scenicAreaByAreaCode } from '../api/index.js';
import { getStoreId } from './utils.js';

const id = getStoreId('scenicIdsData');
export const scenicIdsDataStore = defineStore(id, {
  state: () => ({
    scenicIds: '',
  }),
  getters: {
    getScenicIds: useAutoGetter({
      getter: (state) => {
        return state.scenicIds;
      },
      action: function () {
        this.getScenicIdsByAreaCode();
      },
    }),
  },
  actions: {
    getScenicIdsByAreaCode: useAutoAction({
      action: async function () {
        const { area_code } = useUserInfoStore();
        const params = { area_code };
        let data = await handleCommonRes(scenicAreaByAreaCode, params);
        data = data
          .split(',')
          .map((item) => (item == '86.51.01.08.09.11' ? '86.51.01.08.09.02' : item))
          // .map((item) => (item.split('.').length > 6 ? item.split('.').slice(0, 6).join('.') : item))
          .join(',');
        return (this.scenicIds = data);
      },
    }),
  },
});
